package DatabaseAccessing;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class TransactionModelController {

    public void addTransaction(CustomerTransaction transObj) {
        Transaction trans = null;
        Session session = SessionFactoryUtil.getInstance();

        try {
            trans = session.beginTransaction();
            session.save(transObj);
            trans.commit();
        } catch (RuntimeException exp) {
            if (trans != null && trans.isActive()) {
                try {
                    trans.rollback();
                    System.err.println("Error(addTransaction) Commit error: "
                            + exp.getMessage());
                } catch (HibernateException hibernateException) {
                    System.err.println("Error(addTransaction) Rollback error: "
                            + hibernateException.getMessage());
                }
            }
        }
    }

    public CustomerTransaction getTransaction(int transactionID) {
        CustomerTransaction customerTrans = null;
        Session session = SessionFactoryUtil.getInstance();

        try {
            customerTrans = (CustomerTransaction) session.get(CustomerTransaction.class, transactionID);
        } catch (HibernateException hibernateException) {
            System.err.println("Error(getTransaction) get error: "
                    + hibernateException.getMessage());
        }

        return customerTrans;
    }

    public List getAllTransactions() {
        List transactions = null;
        Session session = SessionFactoryUtil.getInstance();

        try {
            transactions = session.createQuery("from CustomerTransaction").list();
        } catch (HibernateException hibernateException) {
            System.err.println("Error(getAllTransactions) get error: "
                    + hibernateException.getMessage());
        }

        return transactions;
    }

    public List<CustomerTransaction> getUndoneTransactions(int userID) {
        List transactions = null;
        if (userID != -1) {
            Session session = SessionFactoryUtil.getInstance();
             System.out.println("after session");
            try {
                Criteria nameCriteria = session.createCriteria(CustomerTransaction.class);
                nameCriteria.add(Restrictions.eq("customerId", userID));
                nameCriteria.add(Restrictions.eq("done", false));
                transactions = nameCriteria.list();

            } catch (HibernateException hibernateException) {
                System.err.println("Error(getAllTransactions) get error: "
                        + hibernateException.getMessage());
            }
        }

        return transactions;
    }

    public List getUndoneTransactionsForSpecificProduct(int userID,int productID) {
        List transactions = null;
        if (userID != -1) {
            Session session = SessionFactoryUtil.getInstance();
             System.out.println("after session");
            try {
                Criteria nameCriteria = session.createCriteria(CustomerTransaction.class);
                nameCriteria.add(Restrictions.eq("customerId", userID));
                nameCriteria.add(Restrictions.eq("done", false));
                nameCriteria.add(Restrictions.eq("productId", productID));
                transactions = nameCriteria.list();

            } catch (HibernateException hibernateException) {
                System.err.println("Error(getAllTransactions) get error: "
                        + hibernateException.getMessage());
            }
        }

        return transactions;
    }

    public void setDone(int transID) {
        Transaction trans = null;
        Session session = SessionFactoryUtil.getInstance();

        try {
            trans = session.beginTransaction();
            CustomerTransaction customerTrans = (CustomerTransaction) session.load(CustomerTransaction.class, transID);
            customerTrans.setDone(true);
            trans.commit();
        } catch (RuntimeException exp) {
            if (trans != null && trans.isActive()) {
                try {
                    trans.rollback();
                    System.err.println("Error(setDone) Commit error: "
                            + exp.getMessage());
                } catch (HibernateException hibernateException) {
                    System.err.println("Error(setDone) Rollback error: "
                            + hibernateException.getMessage());
                }
            }
        }
    }

    public void updateTransaction(CustomerTransaction transObj) {
        Transaction trans = null;
        Session session = SessionFactoryUtil.getInstance();

        try {
            trans = session.beginTransaction();
            session.update(transObj);
            trans.commit();
        } catch (RuntimeException exp) {
            if (trans != null && trans.isActive()) {
                try {
                    trans.rollback();
                    System.err.println("Error(updateTransaction) Commit error: "
                            + exp.getMessage());
                } catch (HibernateException hibernateException) {
                    System.err.println("Error(updateTransaction) Rollback error: "
                            + hibernateException.getMessage());
                }
            }
        }
    }

    public void deleteTransaction(int transID) {
        Transaction trans = null;
        Session session = SessionFactoryUtil.getInstance();

        try {
            trans = session.beginTransaction();
            CustomerTransaction customerTrans = (CustomerTransaction) session.load(CustomerTransaction.class, transID);
            session.delete(customerTrans);
            trans.commit();
        } catch (RuntimeException exp) {
            if (trans != null && trans.isActive()) {
                try {
                    trans.rollback();
                    System.err.println("Error(deleteTransaction) Commit error: "
                            + exp.getMessage());
                } catch (HibernateException hibernateException) {
                    System.err.println("Error(deleteTransaction) Rollback error: "
                            + hibernateException.getMessage());
                }
            }
        }
    }
}
